package io.renren.modules.device.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.device.entity.TDeviceThreshold;
import io.renren.modules.device.entity.vo.DeviceThresholdVO;
import io.renren.modules.device.service.TDeviceThresholdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 告警配置接口
 *
 * @author lhb
 * @date 2023/6/1
 */
@RestController
@RequestMapping("threshold")
public class DeviceThresholdController {

    @Autowired
    private TDeviceThresholdService thresholdService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("threshold:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = thresholdService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{code}")
    @RequiresPermissions("threshold:info")
    public R info(@PathVariable("code") String code) {
        DeviceThresholdVO thresholdInfo = thresholdService.getDeviceThresholdInfo(code);

        return R.ok().put("threshold", thresholdInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("threshold:save")
    public R save(@RequestBody TDeviceThreshold tDevice) {

        try {
            thresholdService.save(tDevice);

            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("threshold:update")
    public R update(@RequestBody TDeviceThreshold tDevice) {
        thresholdService.updateById(tDevice);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("threshold:delete")
    public R delete(@RequestBody String[] codes) {
        thresholdService.removeByIds(Arrays.asList(codes));

        return R.ok();
    }
}
